var sellerCode="";
$(document).ready(function() {
	//top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值
	doSearch();
});

function doSearch(){
	var cardId = $("#cardId").val();
	var cardName=$("#cardName").val();
	var title="";
	// 实际提取分数
	$.ajax({
		url : contextPath + '/iccard/report/bznis/package/actDetailCount',
		dataType : "json",
		async:false,
		data : {
			cardId : cardId
		},
		success : function(data) {
			title="套餐名称："+cardName+"  实际销售份数 ： "+data+"份";
		}
	});
	
	$("#data_list").datagrid({
		url : contextPath+"/iccard/report/bznis/package/actDetail",
		title : title,
		onLoadError : function() {
			uppAjaxError(data);
		},
		onLoadSuccess:function(data){
			$("#export_link").attr('href',contextPath+'/iccard/report/temp/'+data.fileName);
		},
		queryParams : {
			cardId : cardId
		},
		onLoadSuccess: function (data) {
			paint();
		},
		columns : [ [ {
			field : 'endCombocardEarningsDetailId',
			title : '',
			hidden : true
		},{
			field : 'merchantName',
			title : '商家名称'
		}, {
			field : 'productNames',
			title : '涉及商品'
		}, {
			field : 'combcardExpenseCopies',
			title : '实际提取份数'
		}, {
			field : 'productOrignalPrice',
			title : '原价格'
		}, {
			field : 'productFavorablePrice',
			title : '优惠价格'
		}, {
			field : 'productSalePrice',
			title : '销售价格'
		},{
			field : 'expenseTotalAmount',
			title : '销售额'
		},{
			field : 'payableAmount',
			title : '应付额'
		},{
			field : 'expectedAmount',
			title : '预期收益额'
		} ] ],
		autoRowHeight : true,
		fitColumns : true,
		pagination : true,
		nowrap : false,
		singleSelect : true,
		rownumbers : true
	});
//
	
	//汇总
	$.ajax({
        url: contextPath + '/iccard/report/bznis/package/actDetailSum',
        dataType: "json",
        data: {
			cardId : cardId
			},
        success: function( data ) {
        	$("#collect_amount").text("套餐卡销售总额 ："+data.salesTotal+"元                结算总额： "+data.settlementTotal+"元     预期收益总额：XXX元      实际收益总额："+data.incomeTotal+"元 ");
        }
      });
	//
}


function paint(){
	var data=$("#data_list").datagrid("getRows");
//	alert(data);
	//套餐
	var packages = new Array();
	//销售
	var sales = new Array();
	//收益
	var income = new Array();
	//销售总额
	var sales_total =0.0;
	//收益总额
	var income_total =0.0;
	//chat
	var chat_sales = new Array("",0.0);
	var chat_income = new Array("",0.0);
	for(var i = 0;i<data.length;i++){
		packages[i] = data[i].merchantName;
		sales[i] = data[i].expenseTotalAmount;
		income[i] = data[i].expectedAmount;
		sales_total += data[i].expenseTotalAmount;
		income_total += data[i].expectedAmount;
	}
	for(var i = 0;i<sales.length;i++){
		chat_sales[i] =new Array( packages[i],100*sales[i]/sales_total);
		chat_income[i] =new Array( packages[i],100*income[i]/income_total);
	}
    //饼图
    $('#sales_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '销售额'
        },
        subtitle: {
            text: ''
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: chat_sales
        }]
    });
    
    $('#income_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '收益额'
        },
        subtitle: {
            text: ''
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: chat_income
        }]
    });
}
